using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Entity.Particle;
using Spinning.Spinning.Interface;
using Spinning.Spinning.Unitity;

namespace Spinning.Spinning.Level
{
    class RealEnemyLuncher : ParticleLuncher
    {
        PracticeScene _scene;

        public RealEnemyLuncher(int capacity, PracticeScene scene)
            : base(capacity)
        {
            this._scene = scene;
            GenerateParticles();
        }

        protected override void ShotParticle(IParticle particle, Microsoft.Xna.Framework.Vector2 pos)
        {
            float nv = ((float)random.Next(70, 120)) / 5f;
            Vector2 vel = get_UnitV_ByActor(_scene.Player, pos) * nv;
            int a = 25;
            float aX = ((float)random.Next(-a, a)) / 20f;
            float aY = ((float)random.Next(-a, a)) / 10f;
            Vector2 acc = new Vector2(aX, aY);

            particle.ParticlePosition = pos;
            particle.Velocity = vel;
            particle.Acceleration = acc;
            particle.isMotor = true;
        }

        protected override IParticle GenerateParticle()
        {
            FireEnemy enemy = new FireEnemy();
            enemy.textureName = "spinning";
            enemy.OnLoad();
            enemy.Origin = new Vector2(0.5f);
            enemy.Style = 1;
            enemy.SetAnimation(Constants.playerAnimationFrame);
            enemy.OnCreatBody(_scene.World, _scene.PhysicConnectManager);
            enemy.OnBeginContactEvent += onParticleContact;
            _scene.TouchManager.RegisteTouchArea(enemy);
            _scene.Attach(enemy);
            return enemy;
        }

        private Vector2 get_UnitV_ByActor(Actor entity, Vector2 pos)
        {
            int offset = 35;
            Vector2 v = entity.Position - pos + new Vector2(random.Next(-offset, offset), random.Next(-offset, offset));
            v.Normalize();
            return v;
        }

        void onParticleContact(PhysicSprite me, PhysicSprite _other)
        {
            Actor other = (Actor)_other;
            ParticleActor e = (ParticleActor)me;
            if (!e.isDead&&e.inScreen())
            {
                if (other.Physic_Type == Spinning.Entity.Actor.PhysicType.Player ||
                    other.Physic_Type == Spinning.Entity.Actor.PhysicType.Enemy ||
                    other.Physic_Type == Spinning.Entity.Actor.PhysicType.Marble)
                {
                    e.isMotor = false;
                    if (other.Physic_Type == Spinning.Entity.Actor.PhysicType.Player)
                        e.isHit = true;
                }
            }

        }

    }
}
